# 检测异动,方便打板
from datetime import datetime

import pandas as pd
import requests

from core.AppContext import AppContext
from core.Constants import UNIT_WAN, EVENT_BIG_BUY
from core.TimeComponent import TimeComponent
from core.componet_decorator import component
from core.engine import Event
from core.time_decorator import TimeDecorator
from util.stock_util import is_main_board


def stock_changes_em(symbol: str = "大笔买入") -> pd.DataFrame:
    """
    东方财富-行情中心-盘口异动
    https://quote.eastmoney.com/changes/
    :param symbol: choice of {'火箭发射', '快速反弹', '大笔买入', '封涨停板', '打开跌停板', '有大买盘',
    '竞价上涨', '高开5日线', '向上缺口', '60日新高', '60日大幅上涨', '加速下跌', '高台跳水',
    '大笔卖出', '封跌停板', '打开涨停板', '有大卖盘', '竞价下跌', '低开5日线', '向下缺口', '60日新低', '60日大幅下跌'}
    :type symbol: str
    :return: 盘口异动
    :rtype: pandas.DataFrame
    """
    url = "https://push2ex.eastmoney.com/getAllStockChanges"
    symbol_map = {
        "火箭发射": "8201",
        "快速反弹": "8202",
        "大笔买入": "8193",
        "封涨停板": "4",
        "打开跌停板": "32",
        "有大买盘": "64",
        "竞价上涨": "8207",
        "高开5日线": "8209",
        "向上缺口": "8211",
        "60日新高": "8213",
        "60日大幅上涨": "8215",
        "加速下跌": "8204",
        "高台跳水": "8203",
        "大笔卖出": "8194",
        "封跌停板": "8",
        "打开涨停板": "16",
        "有大卖盘": "128",
        "竞价下跌": "8208",
        "低开5日线": "8210",
        "向下缺口": "8212",
        "60日新低": "8214",
        "60日大幅下跌": "8216",
    }
    reversed_symbol_map = {v: k for k, v in symbol_map.items()}
    params = {
        "type": symbol_map[symbol],
        "pageindex": "0",
        "pagesize": "10",
        "ut": "7eea3edcaed734bea9cbfc24409ed989",
        "dpt": "wzchanges",
    }
    r = requests.get(url, params=params)
    data_json = r.json()
    temp_df = pd.DataFrame(data_json["data"]["allstock"])
    temp_df["tm"] = pd.to_datetime(temp_df["tm"], format="%H%M%S").dt.time
    temp_df.columns = [
        "时间",
        "代码",
        "_",
        "名称",
        "板块",
        "相关信息",
    ]
    temp_df = temp_df[
        [
            "时间",
            "代码",
            "名称",
            "板块",
            "相关信息",
        ]
    ]
    # 示例：按逗号分隔成4列
    temp_df[['手数', '均价', 'a', '金额']] = temp_df['相关信息'].str.split(',', expand=True)
    # 批量转换为数字（浮点数）
    temp_df[['手数', '均价', 'a', '金额']] = temp_df[['手数', '均价', 'a', '金额']].apply(pd.to_numeric,
                                                                                          errors='coerce')
    # 方法1：直接指定列名列表删除（返回新DataFrame）
    temp_df = temp_df.drop(columns=['相关信息', '板块', 'a'])
    temp_df['金额'] = temp_df['金额'] / UNIT_WAN
    return temp_df


@component(True)
class stock_changes_big_money(TimeComponent):
    def __init__(self):
        print("------->")
        self.prev_filtered_stocks = None
        self.cache = {}
        self.today_str = datetime.today().strftime('%Y%m%d')
        self.zhang_tai_duo = []

    def job(self):
        dbmr = stock_changes_em(symbol="大笔买入")

        # 过滤主板股票且最新价<100元
        filtered_stocks = dbmr[
            (dbmr['代码'].apply(is_main_board)) &  # 主板
            (dbmr['金额'] > 500) &  #
            (dbmr['均价'] < 100)
            ]

        AppContext.get_app_context().engine.put(Event(EVENT_BIG_BUY, filtered_stocks))

        self.prev_filtered_stocks = dbmr

    @TimeDecorator(5)
    def update(self, time_event):
        # 获取当前时间（北京时间，UTC+8）
        now = datetime.now()  # 或 datetime.utcnow() + timedelta(hours=8)
        # 格式化为字符串
        formatted_time = now.strftime("%H:%M:%S")  # 2025-08-04 21:33:43
        print("运行时间 ：" + formatted_time)

        # self.job()
